
package com.starshineworks.entities;

import com.db4o.ObjectContainer;
import com.starshineworks.util.DbUtil;
import com.starshineworks.util.Util;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/*enum Status {
    SUBMITTED,
    GRANTED,
    REJECTED
}*/

public class RequestE extends EntityE {
    
    public Integer _userId;
    public Integer _teamId;
    public Integer _status;
    public Integer _sessions;
    public String  _startdate;
    public String  _field1, _field2;
    public String  _day1[], _day2[];
    public String  _time1, _time2;
    
    public static final int SUBMITTED = 1;
    public static final int GRANTED   = 2;
    public static final int REJECTED  = 3;

    public RequestE() {
    }
    
    public RequestE(Integer id) {
        this._id     = id;
        this._status = SUBMITTED;
    }
    
    public static Integer create1(Integer uid, 
                                  Integer tid, 
                                  Map<String,String[]> map) {
        map = Util.adjustMapNames(map);
        ObjectContainer client = DbUtil.openClient();
        RequestE r = new RequestE(Util.nextId());
        r._userId = uid;
        r._teamId = tid;
        r._name = "R" + r._id.toString();
        client.store(r);
        client.close();
        EntityE.updateObjectFields(r, map);
        return r._id;
    }

    @Override
    public String refreshDisplay(int view) {
        _display = "XXXX Name is " + _name;
        if (view != Util.HTMLViewNone) {
            List<List<String>> t = new ArrayList<List<String>>();
            ArrayList<String> keys = new ArrayList<String>();
            keys.add("Request name");
            keys.add("ID");
            keys.add("Team");
            keys.add("Requestor");
            keys.add("Sessions per week");
            keys.add("Start date");
            keys.add(" ");
            keys.add("Field");
            keys.add("Time");
            keys.add("Days");
            
            // Pref column
            ArrayList<String> vals = new ArrayList<String>();
            vals.add(_name);
            vals.add(_id.toString());
            vals.add(TeamE.teamName(_teamId));
            vals.add(UserE.fullName(_userId));
            vals.add(_sessions.toString());
            vals.add(_startdate);
            vals.add("PREFERRED");
            vals.add(_field1);
            vals.add(_time1);
            String days1 = "";
            int dlen = _day1.length;
            int i=0;
            for (; i<dlen; i++) {
                days1 += _day1[i] + " ";
            }
            vals.add(days1);
            t.add(vals);

            // Alt column, bay be absent
            ArrayList<String> vals2 = new ArrayList<String>();
            try {
                vals2.add("");
                vals2.add("");
                vals2.add(_teamId.toString());
                vals2.add(_userId.toString());
                vals2.add("");
                vals2.add("");
                vals2.add("ALTERNATE");
                vals2.add(_field2);
                vals2.add(_time2);
                String days2 = "";
                dlen = _day2.length;
                i = 0;
                for (; i < dlen; i++) {
                    days2 += _day2[i] + " ";
                }
                vals2.add(days2);
            } catch (Exception e) {
                vals2.clear();
                for (i=0; i < 8; i++) {
                    vals2.add("");
                }
                vals2.set(5, "ALTERNATE");
            }
            t.add(vals2);
            if (view == Util.HTMLViewTable) {
                _display = Util.asHTMLTable(keys, t);
                return _display;
            }
        }
        return _display;
    }
}
